#include "SendAPI.h"

int init()
{
	WSADATA wsa;
	WORD ver = MAKEWORD(2, 2);

	if(WSAStartup(ver, &wsa)!=0)	
		return ERROR_INIT_WSA_FAIL;
	

	int code = 0;
	char buf[50];
	memset(buf, 0, sizeof(buf));

	int res;
	if((res=connectToServer())!=SUCCESS)
		return res;
	

	if(sendCmdAndGetRsp(HELLO_STR)!=SERVER_RSP)
		return ERROR_HELLO_FAIL;

	return SUCCESS;
}

int auth(const char* user, const char* passwd)
{
	int code;
	code = sendCmdAndGetRsp(AUTH_STR);

	if (code!=SERVER_AUTH)
		return ERROR_AUTH_FAIL;

	const char *tmp = getUserName(user);	

	char * formatUser = (char *)calloc(sizeof(char), 10+strlen(tmp));
	sprintf(formatUser, "%s\r\n", tmp);
	free((void*)tmp);

	code = sendCmdAndGetRsp(formatUser);

	int tryTimes = 1;
	for (;tryTimes<5&&code==SERVER_BUSY;tryTimes++)
	{
		code = sendCmdAndGetRsp(AUTH_STR);

		if (code!=SERVER_AUTH)
		{
			free(formatUser);
			return ERROR_AUTH_FAIL;
		}

		code = sendCmdAndGetRsp(formatUser);
	}

	free(formatUser);

	tmp = getPassword(passwd);
	char *formatPasswd = (char*)calloc(sizeof(char), 10+strlen(tmp));
	sprintf(formatPasswd, "%s\r\n", tmp);
	code = sendCmdAndGetRsp(formatPasswd);
	free(formatPasswd);

	if (code!=SERVER_AUTH_SUCCESS)
		return ERROR_AUTH_FAIL;

	return SUCCESS;
}

int setSender(const char* sender)
{
	int code ;
	char formatSender[128] = {0};
	getSenderFormatAddr(formatSender, sender);

	code = sendCmdAndGetRsp(formatSender);
	if(code!=SERVER_RSP)
		return ERROR_SET_SENDER_FAIL;
	
	return SUCCESS;
}

int setDst(const char* dst)
{
	int code;
	char formatDst[128] = {0};
	getDstFormatAddr(formatDst, dst);

	code = sendCmdAndGetRsp(formatDst);

	if (code!=SERVER_RSP)
		return ERROR_SET_DST_FAIL;

	return SUCCESS;
}

int setText(const char* title, 
				  const char *content, 
				  const char *sender,
				  const char *dst)
{
	int code;

	code = sendCmdAndGetRsp(DATA_STR);
	if (code!=SERVER_DATA_START)
		return ERROR_SET_TEXT_FAIL;

	char formatText[1024*256] = {0};

	getFormatText(formatText, title, content, sender, dst);
	
	code = sendCmdAndGetRsp(formatText);

	if (code!=SERVER_RSP)
		return ERROR_SET_TEXT_FAIL;

	return SUCCESS;
}

void release()
{
	releaseResource();
}

int __declspec(dllexport) SendEmail(const char* smtpServer,
														  int smtpPort,
														  const char* user,
														  const char* passwd,
														  const char* from,
														  const char* dst,
														  const char* title, 
														  const char* content)
{
	int code;
	if ((code=init())!=SUCCESS)
		goto finish;
	if ((code=auth(user, passwd))!=SUCCESS)
		goto finish;
	if ((code=setSender(from))!=SUCCESS)
		goto finish;
	if((code=setDst(dst))!=SUCCESS)
		goto finish;
	if((code=setText(title, content, from, dst))!=SUCCESS)
		goto finish;

	sendCmdAndGetRsp(QUIT_STR);
	releaseResource();
	return SUCCESS;

finish:
	releaseResource();
	return code;
}